/**
 * Copyright (C) 2010 Neofonie GmbH
 *
 * This programm is free software; you can redistribute it and/or modify
 * it under the terms of the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.dicodeproject.analysis.histogram;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.util.ToolRunner;
import org.apache.lucene.analysis.Analyzer;
import org.apache.mahout.common.*;
import org.apache.mahout.common.commandline.DefaultOptionCreator;
import org.apache.mahout.vectorizer.DefaultAnalyzer;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;

/**
 * Generates a json formatted file that depicts the histogram for one source query we are following
 * over time.
 *
 * Formatting:
 *
 * [[x,y],[x.x,y.y],[x,y.y]]
 */
public class HistogramDriver extends AbstractJob {

  public static final String QUERY_TERMS = "QUERY_TERMS";

  public static void main(String args[]) throws Exception {
    ToolRunner.run(new HistogramDriver(), args);
  }

  @Override
  public int run(String[] args) throws Exception {
    addOutputOption();
    addOption(DefaultOptionCreator.numReducersOption().create());
    addOption("queryTerms", "q", "The query terms to generate histograms for, terms separated by \":\".", "dicode");
    addOption("table", "t", "The hbase table holding our data.", "twittertracker");
    addOption("family", "f", "The column family holding our text data.", "textFamily");
    addOption("column", "c", "The column holding our text data.", "text");
    addOption("familyT", "y", "The column family holding our timestamps.", "metaFamily");
    addOption("columnT", "z", "The column holding our timestamps.", "creationDate");


    Map<String, String> argMap = parseArguments(args);
    if (argMap == null) {
      return -1;
    }

    String queryTerms = argMap.get("--queryTerms");
    String table = argMap.get("--table");
    String textFamily = argMap.get("--family");
    String textColumn = argMap.get("--column");
    String timeFamily = argMap.get("--familyT");
    String timeColumn = argMap.get("--columnT");

    Path output = getOutputPath();
    generateHistogramData(table, textFamily, textColumn, timeFamily, timeColumn, output, queryTerms);

    return 0;
  }

  private void generateHistogramData(final String table, final String textFamily, final String textCol,
                                     final String timeFamily, final String timeCol, final Path output,
                                     final String queryTerms)
      throws IOException, InterruptedException, ClassNotFoundException {
    Configuration conf = HBaseConfiguration.create();
    conf.set(QUERY_TERMS, queryTerms);

    Job job = new Job(conf);
    job.setJarByClass(HistogramDriver.class);

    Scan scan = new Scan();
    scan.addColumn(Bytes.toBytes(textFamily), Bytes.toBytes(textCol));
    scan.addColumn(Bytes.toBytes(timeFamily), Bytes.toBytes(timeCol));
    scan.setMaxVersions(1);
    TableMapReduceUtil.initTableMapperJob(table, scan, HBaseHistogramMapper.class, IntegerTuple.class,
        IntWritable.class, job);

    job.setJobName("HBaseDocumentProcessor::DocumentTokenizer");

    job.setReducerClass(HistogramReducer.class);

    job.setOutputKeyClass(IntegerTuple.class);
    job.setOutputValueClass(IntWritable.class);

    FileOutputFormat.setOutputPath(job, output);
    job.setOutputFormatClass(SequenceFileOutputFormat.class);

    job.setNumReduceTasks(10);
    HadoopUtil.overwriteOutput(output);

    job.waitForCompletion(true);
  }

}
